<template>
  <div class="view-wrapper">
    <div class="head">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="">
          <el-input v-model="formInline.title" placeholder="请输入文章标题名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button  @click="search" type="primary">搜索</el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="listBox">
      <div class="listBox_head">
        <el-button  @click="handleAdd" type="primary">新增公告</el-button>
      </div>
      <ex-table ref="table" :loading="loading" :table-column="tableColumn" :table-data="tableData"
        @pageSizeChange="handleSizeChange" @currentPageChange="handleCurrentChange">
        <template v-slot:updateTime="{ scope }">
          <div >{{scope.row.popStartTime}}</div>
           <div>{{scope.row.popEndTime}}</div>
        </template>
        <template v-slot:status="{ scope }">
          <div v-if="scope.row.status == 1">显示</div>
           <div v-if="scope.row.status == 2">隐藏</div>
        </template>
        <template v-slot:operate="{ scope }">
          <el-button @click="lookDetail(scope.row.id)" size="small">编辑</el-button>
          <el-button @click="deactivate(scope.row.id)" size="small">删除</el-button>
          <el-button @click="handleStatus(scope.row)" size="small" v-if="scope.row.status == 2">显示</el-button>
          <el-button @click="handleStatus(scope.row)" size="small" v-if="scope.row.status == 1">隐藏</el-button>
          <el-button @click="handlePush(scope.row)" size="small">APP通知推送</el-button>
        </template>
      </ex-table>
    </div>
  </div>
</template>
<script>
  // components
  import {
    announcementList,
    removeAnnouncement,
    changeStatus,
    announcementPush
  } from "@/api/announcement";
  import handlePageMixin from "@/mixins/handlePageMixin";
  export default {
    name: "",
    components: {

    },
    mixins: [handlePageMixin],
    data() {
      return {
        loading: false,
        formInline: {
          title: "", //标题
        },
        date: [],
        tableData: [],
      
        tableColumn: [{
            prop: "title",
            label: "公告标题"
          },
          {
            prop: "updateTime",
            label: "更新时间",
            width: 100
          },
          {
            prop: "sort",
            label: "排序"
          },
          {
            prop: "updateTime",
            label: "公告弹窗日期",
            width: 180,
            slot: true,
            slotName: "updateTime",
          },
          {
            prop: "status",
            label: "状态",
            slot: true,
            slotName: "status",
          },
          {
            label: "操作",
            slot: true,
            slotName: "operate",
            fixed: "right",
          },
        ],
      };
    },
    computed: {
      
    },
    created() {},
    mounted() {
      this.getList();
    },
    methods: {

      search() {
        //搜索
        console.log(this.formInline);
        this.formInline.transactionStartTime = this.date[0];
        this.formInline.transactionEndTime = this.date[1];
        this.pageOptions.pageNum = 1;
        this.$refs.table.pageOptions.pageNum = 1;
        this.getList();
      },
      reset() {
        this.formInline = {};
        this.date = [];
        this.pageOptions.pageNum = 1;
        this.$refs.table.pageOptions.pageNum = 1;
        this.getList();
      },
      getList() {

        const params = {
          ...this.pageOptions,
          ...this.formInline,
        };
        this.loading = true;
        announcementList(params)
          .then((res) => {
            this.tableData = res.result.records;
            this.$refs.table.pageOptions.total = res.result.total;
          })
          .finally(() => {
            this.loading = false;
          });
      },
      handleAdd(){
        this.$router.push({
          path: '/incentiveManagement/announcement-detail',
          query: {
            pathType: 'add'
          }
        })
      },
      lookDetail(id) {
        this.$router.push({
          path: '/incentiveManagement/announcement-detail',
          query: {
            pathType: 'edit',
            id: id
          }
        })
      },
      // 删除
      deactivate(id) {
        //删除
        this.$confirm("是否删除？", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
          .then(() => {
            removeAnnouncement(id).then((res) => {
              this.$message({
                message: "删除成功",
                type: "success",
                duration: 1500,
              });
              setTimeout(() => {
                this.reset();
              }, 800);
            });
          })
          .catch(() => {

          });
      },
      //隐藏、显示
      handleStatus(row) {
        var title = ''
        if(row.status == 1){
          title = '是否隐藏'
        }else if(row.status == 2){
          title = '是否显示'
        }
        this.$confirm(title, "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
          .then(() => {
            changeStatus(row.id).then((res) => {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
              });
              setTimeout(() => {
                this.reset();
              }, 800);
            });
          })
          .catch(() => {

          });
      },
      //APP通知推送
      handlePush(row){
        this.$confirm('是否推送APP通知', "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
          .then(() => {
            announcementPush(row.id).then((res) => {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
              });
            });
          })
          .catch(() => {

          });
      },
    },
  };

</script>
<style lang="scss" scoped>
  .el-card-title {
    font-size: 20px;
  }

  .head {
    //   box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
    background-color: #fff;
    border-radius: 8px;
    //   height: 120px;
    padding: 20px 20px 0;
  }

  .el-form--inline .el-form-item {
    margin-right: 20px;
  }
  .listBox_head{
    padding-bottom: 20px;
  }
  .listBox {
    //   box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
    background-color: #fff;
    border-radius: 8px;
    //   height: 120px;
    padding: 20px 20px;
    margin-top: 10px;
  }

  

</style>
